package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;

import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.util.HibernateUtil;

public class ResourceDAO {
	/*
	 * 
	 */
	public Resource createResource(Resource resource){
	try{
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.getTransaction().begin();
	System.out.println("FLUSH MODE: " + session.getFlushMode());
	session.setFlushMode(FlushMode.COMMIT);
	session.save(resource);
	session.getTransaction().commit();
	}catch(Exception e){
		return null;
	}
	return resource;
	
	}
	
	/*
	 * 
	 */
	public boolean saveResources(List<Resource> resourcesList){
		boolean resourcesCreated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			for(Resource resource : resourcesList){
				session.save(resource);
			}
			session.getTransaction().commit();
			resourcesCreated = true;
		}catch(Exception e){
			e.printStackTrace();
			return resourcesCreated;
			
		}
		return resourcesCreated;
	}
	/*
	 * 
	 */
	public boolean isResourceNameAvailable(String resourceName){
		boolean isResourceNameAvailable = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from Resource r where r.resourceName=:resourceName";
			Query query = session.createQuery(queryString).setString("resourceName", resourceName);
			if(query.uniqueResult() == null){
				isResourceNameAvailable = true;
			}else{
			    isResourceNameAvailable = false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return isResourceNameAvailable;
	}
	/*
	 * 
	 */
	public boolean updateResource(Resource resource){
		boolean isResourceUpdated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.update(resource);
			session.getTransaction().commit();
			isResourceUpdated = true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return isResourceUpdated;
		
	}
	
	public List<Resource> getResources(Long moduleId){
		List<Resource> resources = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			String queryString = "from Resource r where r.module.moduleId = :moduleId";
			
			Query query = session.createQuery(queryString).setParameter("moduleId", moduleId);
			query.setCacheable(false);
			resources = (List<Resource>) query.list();	
			
			session.close();
			return resources;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	public List<Resource> getResourcesByServiceId(Long serviceId){
		List<Resource> resources = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			String queryString = "select sr.resource from ServiceResource sr where sr.service.serviceId = :serviceId";
			
			Query query = session.createQuery(queryString).setParameter("serviceId", serviceId);
			query.setCacheable(false);
			resources = (List<Resource>) query.list();	
			
			session.close();
			return resources;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	public Resource getResourceById(Long resourceId)
	{
		Resource resource = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			resource = (Resource) session.load(Resource.class, resourceId);
			session.close();
			return resource;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	
	
	public static void main(String a[]){
     Resource resource  = new Resource();
     System.out.println("RESOURCE : "+ new ResourceDAO().createResource(resource));
     System.out.println("RESOURCE ID: "+ resource.getResourceId());
     
	}

}
